package com.company.oa.hr.controller.employee;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.oa.common.api.Result;
import com.company.oa.hr.entity.employee.Attendance;
import com.company.oa.hr.service.employee.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/hr/employee/attendance")
public class AttendanceController {

    @Autowired
    private AttendanceService attendanceService;

    /**
     * 分页查询考勤记录
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('hr:employee:attendance:list')")
    public Result<IPage<Map<String, Object>>> getAttendancePage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            Attendance attendance) {
        IPage<Map<String, Object>> pageResult = attendanceService.getAttendancePage(page, size, attendance);
        return Result.success(pageResult);
    }

    /**
     * 员工签到
     */
    @PostMapping("/check-in")
    public Result<Boolean> checkIn(@RequestParam Long employeeId) {
        boolean success = attendanceService.checkIn(employeeId);
        return Result.success(success);
    }

    /**
     * 员工签退
     */
    @PostMapping("/check-out")
    public Result<Boolean> checkOut(@RequestParam Long employeeId) {
        boolean success = attendanceService.checkOut(employeeId);
        return Result.success(success);
    }

    /**
     * 获取员工某日考勤记录
     */
    @GetMapping("/employee/{employeeId}/date")
    @PreAuthorize("hasAuthority('hr:employee:attendance:info')")
    public Result<Attendance> getEmployeeAttendanceByDate(
            @PathVariable Long employeeId,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        Attendance attendance = attendanceService.getEmployeeAttendanceByDate(employeeId, date);
        return Result.success(attendance);
    }

    /**
     * 获取员工某月考勤统计
     */
    @GetMapping("/employee/{employeeId}/month")
    @PreAuthorize("hasAuthority('hr:employee:attendance:stats')")
    public Result<Map<String, Object>> getEmployeeMonthlyAttendance(
            @PathVariable Long employeeId,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM") Date month) {
        Map<String, Object> stats = attendanceService.getEmployeeMonthlyAttendance(employeeId, month);
        return Result.success(stats);
    }

    /**
     * 批量导入考勤记录
     */
    @PostMapping("/batch-import")
    @PreAuthorize("hasAuthority('hr:employee:attendance:import')")
    public Result<Boolean> batchImportAttendance(@RequestBody List<Attendance> attendanceList) {
        boolean success = attendanceService.batchImportAttendance(attendanceList);
        return Result.success(success);
    }
}